﻿Namespace MiniMap

    Module mMiniMap

        Public Enum MiniMapState

            Closed = 0
            Small = 1
            Large = 2

        End Enum

        Public CurrentFmmapIndex As UInt16

        Public WithEvents MiniMap_Closed As GUI.MirWindow
        Public WithEvents MiniMap_Small As GUI.MirWindow
        Public WithEvents MiniMap_Large As GUI.MirWindow

        Private mState As MiniMapState

        Private mTransparentButton_Small As Rectangle
        Private mSizeButton_Small As Rectangle

        Private mTransparentButton_Large As Rectangle
        Private mSizeButton_Large As Rectangle

        Private IsTransparent As Boolean

        Private MMap_X_Ratio As Single
        Private MMap_Y_Ratio As Single

        Public Sub Init()

            With Libs.GameInter.Image(145).Header
                MiniMap_Closed = New GUI.MirWindow("MiniMap_Closed", DirectX.DispMode.Width - .Width, 0, .Width, .Height, False)
            End With

            MiniMap_Small = New GUI.MirWindow("MiniMap_Small", DirectX.DispMode.Width - 128, 0, 128, 128)
            MiniMap_Large = New GUI.MirWindow("MiniMap_Large", DirectX.DispMode.Width - 256, 0, 256, 256)

            mTransparentButton_Small = New Rectangle(130 - Libs.GameInter.Image(144).Header.Width, 2, 16, 16)
            mSizeButton_Small = New Rectangle(130 - Libs.GameInter.Image(144).Header.Width, 18, 16, 16)

            mTransparentButton_Large = New Rectangle(258 - Libs.GameInter.Image(144).Header.Width, 2, 16, 16)
            mSizeButton_Large = New Rectangle(258 - Libs.GameInter.Image(144).Header.Width, 18, 16, 16)

            Windows.AddWindow(MiniMap_Closed, Windows.WindowType.Normal)

        End Sub

        Public Sub Shutdown()

            MiniMap_Closed.Destory()

        End Sub

        Public Sub Toggle()

            If mState = MiniMapState.Closed Then
                mState = MiniMapState.Small
                Windows.RemoveWindow(MiniMap_Closed)
                Windows.AddWindow(MiniMap_Small, Windows.WindowType.AlwaysOnTop)
            Else
                If mState = MiniMapState.Small Then Windows.RemoveWindow(MiniMap_Small)
                If mState = MiniMapState.Large Then Windows.RemoveWindow(MiniMap_Large)
                mState = MiniMapState.Closed
                Windows.AddWindow(MiniMap_Closed, Windows.WindowType.AlwaysOnTop)
            End If

        End Sub

        Public Sub ToggleSize()

            If mState = MiniMapState.Small Then
                mState = MiniMapState.Large
                Windows.RemoveWindow(MiniMap_Small)
                Windows.AddWindow(MiniMap_Large, Windows.WindowType.AlwaysOnTop)
            ElseIf mState = MiniMapState.Large Then
                mState = MiniMapState.Small
                Windows.RemoveWindow(MiniMap_Large)
                Windows.AddWindow(MiniMap_Small, Windows.WindowType.AlwaysOnTop)
            End If

        End Sub
        Private Sub MiniMap_Closed_DrawWindow(ByRef mode As Globals.mGlobalVars.DrawingMode, ByRef x As Integer, ByRef y As Integer, ByRef z As Single) Handles MiniMap_Closed.DrawWindow

            If mState = MiniMapState.Closed Then

                If mode = Globals.DrawingMode.Normal Then

                    Libs.GameInter.Image(145).Draw(x, y, z)

                ElseIf mode = Globals.DrawingMode.Text Then

                    If Not GameWindow.mCurrentMapName Is Nothing Then DirectX.ProFont9.DrawNormalText(GameWindow.mCurrentMapName, x + (67 - (DirectX.ProFont9.GetNormalTextWidth(GameWindow.mCurrentMapName) / 2)), y + 9, Color.White, z)
                    DirectX.ProFont9.DrawNormalText(GameWindow.CoordX & " : " & GameWindow.CoordY, x + (67 - (DirectX.ProFont9.GetNormalTextWidth(GameWindow.CoordX & " : " & GameWindow.CoordY) / 2)), y + 27, Color.White, z)

                End If

            End If

        End Sub

        Private Sub MiniMap_Small_DrawWindow(ByRef mode As Globals.DrawingMode, ByRef x As Integer, ByRef y As Integer, ByRef z As Single) Handles MiniMap_Small.DrawWindow

            If mState = MiniMapState.Small Then

                If CurrentFmmapIndex = 0 Then Toggle()

                DrawMiniMap(mode, x, y, z, 128, 128)

            End If

        End Sub

        Private Sub DrawMiniMap(ByRef mode As Globals.DrawingMode, ByRef x As Integer, ByRef y As Integer, ByRef z As Single, ByRef w As Integer, ByRef h As Integer)

            If CurrentFmmapIndex <> 0 Then

                With Libs.Fmmap.Image(CurrentFmmapIndex)

                    Dim locX As Integer, locY As Integer

                    MMap_X_Ratio = (CSng(.Header.Width) / CSng(GameWindow.mCurrentMap.mWidth))
                    MMap_Y_Ratio = (CSng(.Header.Height) / CSng(GameWindow.mCurrentMap.mHeight))

                    locX = CInt(CSng(GameWindow.CoordX) * MMap_X_Ratio) - (w / 2)
                    locY = CInt(CSng(GameWindow.CoordY) * MMap_Y_Ratio) - (h / 2)

                    Dim oX As Integer, oY As Integer

                    If locX < 0 Then
                        oX = 0 - locX
                        locX = 0
                    End If
                    If locY < 0 Then
                        oY = 0 - locY
                        locY = 0
                    End If

                    Dim R As Rectangle = New Rectangle(locX, locY, w, h)

                    If IsTransparent Then
                        If mode = Globals.DrawingMode.SemiTransparent Then DirectX.GameSprite.Draw(.Texture, R, New Microsoft.DirectX.Vector3(0.0F, 0.0F, 0.0F), New Microsoft.DirectX.Vector3(x + oX, y + oY, z + 0.001F), Color.White)
                    Else
                        If mode = Globals.DrawingMode.Normal Then DirectX.GameSprite.Draw(.Texture, R, New Microsoft.DirectX.Vector3(0.0F, 0.0F, 0.0F), New Microsoft.DirectX.Vector3(x, y, z), Color.White)
                    End If

                End With

            End If

            If mode = Globals.DrawingMode.Normal Then

                With Libs.GameInter

                    .Image(135).Draw(x, y, z)
                    .Image(136).Draw(x, (y + h) - 25, z)
                    .Image(137).Draw((x + w) - 25, (y + h) - 25, z)

                    '3 buttons, main image
                    .Image(144).Draw((x + w) - .Image(144).Header.Width, y, z)

                    'Transparency Button
                    If IsTransparent Then
                        .Image(140).Draw((x + w + 2) - .Image(144).Header.Width, y + 2, z)
                    End If

                    If mState = MiniMapState.Small Then
                        .Image(143).Draw((x + w + 2) - .Image(144).Header.Width, y + 18, z)
                    End If

                End With

            ElseIf mode = Globals.DrawingMode.Line Then

                If CurrentFmmapIndex = 0 Then
                    Engine.DrawSolidBox(x, x + w, y, y + h, z, Color.Black)
                End If

                Engine.DrawBox(x, x + w, y, y + h, z, 1.0F, Color.Gray)

                Engine.DrawSolidBox(x + (w / 2) - 1, x + (w / 2) + 1, y + (h / 2) - 1, y + (h / 2) + 1, z, Color.LawnGreen)

                For Each M As Mobs.Mob In Mobs.lstMobs

                    If M.VitalPoints.HP > 0 Then

                        With Libs.Fmmap.Image(CurrentFmmapIndex)

                            Dim locX As Integer, locY As Integer

                            locX = (M.PositionInfo.INT2_X - GameWindow.CoordX) * MMap_X_Ratio
                            locY = (M.PositionInfo.INT2_Y - GameWindow.CoordY) * MMap_Y_Ratio

                            Engine.DrawSolidBox(x + (w / 2) + locX - 1, x + (w / 2) + 1 + locX, y + (h / 2) + locY - 1, y + (h / 2) + locY + 1, z, Color.Red)

                        End With

                    End If

                Next

                For Each N As NPCs.NPCDesc In NPCs.lstNPC.Values

                    Dim locX As Integer, locY As Integer

                    With Libs.Fmmap.Image(CurrentFmmapIndex)

                        locX = (N.mXCoord - GameWindow.CoordX) * MMap_X_Ratio
                        locY = (N.mYCoord - GameWindow.CoordY) * MMap_Y_Ratio

                        Engine.DrawSolidBox(x + (w / 2) + locX - 1, x + (w / 2) + 1 + locX, y + (h / 2) + locY - 1, y + (h / 2) + locY + 1, z, Color.Yellow)
                    End With

                Next

                For Each A As Actor.MirActor In Characters.lstCharacters

                    Dim locX As Integer, locY As Integer

                    With Libs.Fmmap.Image(CurrentFmmapIndex)
                        
                        locX = (A.mXCoord - GameWindow.CoordX) * MMap_X_Ratio
                        locY = (A.mYCoord - GameWindow.CoordY) * MMap_Y_Ratio

                        Engine.DrawSolidBox(x + (w / 2) + locX - 1, x + (w / 2) + 1 + locX, y + (h / 2) + locY - 1, y + (h / 2) + locY + 1, z, Color.White)
                    End With

                Next

                '///////////////TOKENS
                For Each F As FloorItem In FloorItems.lstItems.Values

                    'check if item is token
                    If F.INT_ID = -1 Then

                        'TODO: (Client) Upgrade all draw boxy bits in this sub to the new sub DrawMiniMapBox()
                        DrawMiniMapBox(F.INT2_X, F.INT2_Y, w, h, z, Color.HotPink)

                    End If


                Next

            End If


        End Sub

        Private Sub DrawMiniMapBox(x As Integer, y As Integer, w As Integer, h As Integer, z As Single, col As Color)

            Dim locX As Integer, locY As Integer
            locX = (x - GameWindow.CoordX) * MMap_X_Ratio
            locY = (y - GameWindow.CoordY) * MMap_Y_Ratio
            Engine.DrawSolidBox(x + (w / 2) + locX - 1, x + (w / 2) + 1 + locX, y + (h / 2) + locY - 1, y + (h / 2) + locY + 1, z, Color.White)

        End Sub

        Private Sub MiniMap_Small_MouseEvent(ByRef type As Mouse.MouseInputType, ByRef e As MouseEventArgs) Handles MiniMap_Small.MouseEvent

            If type = Mouse.MouseInputType.MouseClick Then

                If mTransparentButton_Small.Contains(e.X, e.Y) Then
                    IsTransparent = Not IsTransparent
                End If

                If mSizeButton_Small.Contains(e.X, e.Y) Then
                    ToggleSize()
                    'MessageBoxes.AddMessageBox("ToggleSize", "[toggle size]", MessageBoxes.MsgBoxButtons.OK, Nothing)
                End If

            End If

        End Sub

        Private Sub MiniMap_Large_DrawWindow(ByRef mode As Globals.DrawingMode, ByRef x As Integer, ByRef y As Integer, ByRef z As Single) Handles MiniMap_Large.DrawWindow

            If mState = MiniMapState.Large Then

                If CurrentFmmapIndex = 0 Then Toggle()

                DrawMiniMap(mode, x, y, z, 256, 256)

            End If

        End Sub

        Private Sub MiniMap_Large_MouseEvent(ByRef type As Mouse.MouseInputType, ByRef e As MouseEventArgs) Handles MiniMap_Large.MouseEvent

            If type = Mouse.MouseInputType.MouseClick Then

                If mTransparentButton_Large.Contains(e.X, e.Y) Then
                    IsTransparent = Not IsTransparent
                End If

                If mSizeButton_Large.Contains(e.X, e.Y) Then
                    ToggleSize()
                    'MessageBoxes.AddMessageBox("ToggleSize", "[toggle size]", MessageBoxes.MsgBoxButtons.OK, Nothing)
                End If

            End If

        End Sub

    End Module

End Namespace